package cn.consult.controller.sys.apartment.instruct;

import cn.consult.common.api.CommonResult;
import cn.consult.common.utils.RedisUtils;
import cn.consult.netty.ElectricityMeterUtil;
import cn.consult.netty.InstructConstant;
import cn.consult.netty.InstructTypeConstant;
import cn.consult.sys.entity.TWeEquipment;
import cn.consult.sys.service.TWeEquipmentService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ Author：周xx
 * @ Date：2024-04-11-9:26
 * @ Version：1.0
 * @ Description：
 */
@Api(tags = "电表指令")
@RestController
@RequestMapping("/electricity/instruct")
public class ElectricityInstructController {

    @Autowired
    TWeEquipmentService tWeEquipmentService;
    @Autowired
    RedisUtils redisUtils;

    @ApiOperation("operationElectricity =>  电闸")
    @GetMapping("/operationElectricity/{id}/{type}")
    public CommonResult operationElectricity(@PathVariable Long id, @PathVariable Long type, HttpServletRequest request) {
        boolean flag = tWeEquipmentService.operationElectricity(id, type, request);
        if (flag) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("getParams 二次发送直接获取参数")
    @GetMapping("/{id}")
    public CommonResult getParams(@PathVariable Long id) {
        TWeEquipment byId = tWeEquipmentService.getById(id);
        String instruct = InstructConstant.SUMMARY_INSTRUCTS + byId.getEquipmentNumber();
        if (redisUtils.get(instruct) != null) {
            String jsonString = redisUtils.get(instruct);
            Map<String, Object> map = JSON.parseObject(jsonString);
            return CommonResult.success(map);
        }
        return CommonResult.failed();
    }

//    @ApiOperation("抄表")
//    @GetMapping("/get/{type}")
//    public CommonResult get(@PathVariable Long type) {
//        boolean flag = tWeEquipmentService.get(type);
//        return CommonResult.success();
//    }

}
